# These are some general functions and tests runnable with minicomum.

printNum:
  $0
  0 ><
  @@
    $0 10 % "0" + ><
    10 /

    $0 0 = ?
      !@
    .
  .
  ^
  -->
.

printNumS: # prints number as signed, doesn't pop
  $0 -1 2 / >
  ? # negative?
    "-" ->
    -1 >< - 1 +
  .
  printNum
.

factR: # computes factorial recursively
  ?'
    $0 -- factR *
  ;
    ^ 1
  .
.

factI: # computes factorial iteratively
  $0 --

  @'
    ><
    $1
    *
    ><
    --
  .

  ^
.

isPrime: # checks if a number is prime
  $0 1 <=
  ?
    ^ 0 # 0 and 1 are not primes
  ;
    $0 2 /

    @@ # test divisors
      $0 1 <=
      ?          # no divisors greater than 1 found?
        ^ ^ 1 !@
      .

      $1 $1 % 0 =
      ?          # is divisible?
        ^ ^ 0 !@
      .

      --
    .
  .
.

1 2 3 4 5

0 "stack top: " --> $$ printNum ^ 10 ->

0 "stack top (should be the same): " --> $$ printNum ^ 10 ->

0 "factorial of 6 (recursive and iterative): " -->

6 factR printNum ^ " " ->
6 factI printNum ^ 10 ->

0 10 "primes up to 100: " -->

0 # tested number
@@
  $0 100 = ?
    !@
  .
  
  $0 isPrime ?
    $0 printNum ^ 10 ->
  .

  ++
.

0 "pointer manipulation: " -->

$$ 2 $+0 $$ $4 -  # stack top now: 4
$' $$ >< - + 1 |<

20 = ?
  0 10 "OK" -->
;
  0 10 "ERROR" -->
.
